﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Data;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class CustomerInstrumentList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList(1);
                if (!base.CheckUserHasFuction(1, "Func5092"))
                {
                    btnExport.Visible = false;
                }
            }
        }
        private void BindList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();

            string Whereclause = getQuerysql();
            
            int RecordCount = 0;
            DataTable dt = new MaintainBillBll().GetCustomerInstrumentTable(Whereclause, "InstrumentBeginUse desc", AspNetPager1.PageSize, pageindex, out RecordCount);
            
            RepeaterUsers.DataSource = dt;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
            
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                Label lblcusts = e.Item.FindControl("lblcusts") as Label;
                Label lblinstruments = e.Item.FindControl("lblinstruments") as Label;
                if (lblcusts != null)
                {
                    string Whereclause = getQuerysql();
                    DataRow dr = new MaintainBillBll().TotalCustomerInstrument(Whereclause).Rows[0];
                    lblcusts.Text = dr["custs"].ToString() + "个客户";
                    lblinstruments.Text = dr["instruments"].ToString() + "台仪器";
                }
            }
        }
        private string getQuerysql()
        {
            string Whereclause = "";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause += " and  (CustomerName like '%" + txtContractCodeBasic.Text.Trim() + "%' or  CustomerArea like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }
            if (txtinstrumentName.Text.Trim() != "")
            {
                Whereclause += " and  instrumentName like '%" + txtinstrumentName.Text.Trim() + "%'";

            }
            if (txtSerialNum.Text.Trim() != "")
            {
                Whereclause += " and  SerialNum like '%" + txtSerialNum.Text.Trim() + "%'";

            }

            if (ddlStatusName.SelectedValue != "")
            {
                Whereclause += " and  InstrumentStatusName = '" + ddlStatusName.SelectedValue + "'";
            }

            if (BeginDateFrom.SelectedDate != null)
            {
                Whereclause += " and datediff(day,InstallDate,'" + BeginDateFrom.SelectedDate.ToString() + "')<=0";
            }
            if (BeginDateTo.SelectedDate != null)
            {
                Whereclause += " and datediff(day,InstallDate,'" + BeginDateTo.SelectedDate.ToString() + "')>=0";
            }

            return Whereclause;
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = getQuerysql();

            DataTable dt = new MaintainBillBll().ExportCustomerInstrumentTable(Whereclause, " [开始使用日期] desc ");

            Export.Export2Excel(dt, "客户仪器一览表", Page.Response);

        }

    }
}
